<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
        <!-- 搜索和添加按钮区域 -->
       <el-row :gutter="20">
           <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryInfo.query">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                </el-input>
           </el-col>
           <el-col :span="4">
                <el-button type="primary" @click="goAddpage">添加商品</el-button>
           </el-col>
       </el-row>
        <!-- table表格区域 -->
        <el-table :data="tableData" style="width: 100%" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="goods_name" label="商品名称" width="400"></el-table-column>
            <el-table-column prop="goods_price" label="商品价格（元）"></el-table-column>
            <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
            <el-table-column prop="upd_time" label="创建时间">
                <template slot-scope="scope">
                    {{ scope.row.add_time | dataFormat }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="130">
                <template slot-scope="scope">
                     <el-button type="primary" icon="el-icon-edit" size="small" @click="goEditpage(scope.row.goods_id)"></el-button>
                     <el-button type="danger" icon="el-icon-delete" size="small" @click="removeGoodsById(scope.row.goods_id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <goods-paging :pagenum="queryInfo.pagenum" :pagesize="queryInfo.pagesize"
        :total="total" @NewSize="SetNewsize" @NewPage="SetNewpage"></goods-paging>
    </el-card>
  </div>
</template>
<script>
import goodsPaging from './children/paging'
export default {
  components: {
    goodsPaging
  },
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总条数
      total: 0,
      // 所有商品内容
      tableData: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取所有商品
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('商品列表获取失败')
      this.total = res.data.total
      this.tableData = res.data.goods
    },
    // 根据从paging.vue中获取的新的页码  和  当前页数重新请求和渲染数据
    SetNewsize (newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    SetNewpage (newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsList()
    },
    // 点击进入添加商品
    goAddpage () {
      this.$router.push('/home/goods/add')
    },
    // 点击进入修改页面
    goEditpage (goodsId) {
      this.$router.push('/home/goods/edit/' + goodsId)
    },
    // 点击进行删除操作
    async removeGoodsById (GoodsId) {
      const confirmResult = await this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('取消了删除')
      const { data: res } = await this.$http.delete(`goods/${GoodsId}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg || '删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    }
  }
}
</script>
